package com.xuecheng.learning.controller;

import com.xuecheng.api.learning.CourseRecordApi;
import com.xuecheng.api.learning.model.dto.CourseRecordDTO;
import com.xuecheng.api.learning.model.vo.CourseRecordVo;
import com.xuecheng.api.media.model.ResourceUrlModel;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.uaa.LoginUser;
import com.xuecheng.learning.common.utils.UAASecurityUtil;
import com.xuecheng.learning.convert.CourseRecordConvert;
import com.xuecheng.learning.service.CourseRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 选课记录 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseRecordController implements CourseRecordApi {

    @Autowired
    private CourseRecordService courseRecordService;


    @GetMapping("learnedRecords/myCourseRec/{coursePubId}")
    public CourseRecordDTO getCourseRecordByCoursePubId(@PathVariable Long coursePubId) {

        LoginUser loginUser = UAASecurityUtil.getUser();
        String username = loginUser.getUsername();

        CourseRecordDTO recordDTO = courseRecordService.getCourseRecordByCoursePubId(coursePubId, username);

        return recordDTO;
    }

    @PostMapping("learnedRecords/list")
    public PageVO<CourseRecordDTO> queryCourseRecordList(PageRequestParams params) {

        String username = UAASecurityUtil.getUser().getUsername();

        return courseRecordService.queryCourseRecordList(params, username);
    }

    @PostMapping("learnedRecords/progress")
    public CourseRecordDTO updateCourseRecord(@RequestBody CourseRecordVo recordVo) {
        String username = UAASecurityUtil.getUser().getUsername();
        CourseRecordDTO courseRecordDTO = CourseRecordConvert.INSTANCE.voToDTO(recordVo);
        courseRecordDTO.setUserName(username);
        return courseRecordService.updateCourseRecord(courseRecordDTO);
    }

    //获取视频播放地址
    @GetMapping("resource-url")
    public ResourceUrlModel getResourceUrl(@RequestParam Long coursePubId,
                                           @RequestParam Long teachplanId,
                                           @RequestParam Long mediaId) {

        String username = UAASecurityUtil.getUser().getUsername();
        return courseRecordService.getResourceUrl(coursePubId, teachplanId, mediaId, username);
    }

}
